﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Simple.Common;
using System.Web.Mvc;

namespace Simple.Web.Framework.Infrastructure
{
    public class JsonNetResult : JsonResult
    {
        /// <summary>
        /// 重写JOSN格式转换方法
        /// </summary>
        /// <param name="context"></param>
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if ((this.JsonRequestBehavior == System.Web.Mvc.JsonRequestBehavior.DenyGet) && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("默认不允许使用GET方式");
            }
            HttpResponseBase response = context.HttpContext.Response;
            if (!string.IsNullOrEmpty(this.ContentType))
            {
                response.ContentType = this.ContentType;
            }
            else
            {
                response.ContentType = "application/json";
            }
            if (this.ContentEncoding != null)
            {
                response.ContentEncoding = this.ContentEncoding;
            }
            if (this.Data != null)
            {
                string output = SimpleJsonHelper.Serialize(this.Data);
                response.Write(output);
            }
        }
    }
}